﻿@using System.Configuration
@using System.Web.Configuration
@{
    //if (!Roles.IsUserInRole("admin")) {
    //    Response.Redirect("/Account/Login?returnurl=/");
    //}
    
    Page.Title = "Welcome to Git Web Access Sample Website";
    Layout = "~/_SiteLayout.cshtml";
    <p>This website was built using <a href="http://gitweb.codeplex.com">Git Web Access</a>. </p>
    
    var host = Request.Url.ToString();
    if (Request.Url.PathAndQuery!="/")
    {
        host = host.Substring(0, host.IndexOf(Request.Url.PathAndQuery)) + "/";
    }
    
    var errorMessage = "";
    var isValid = true;
    var gitExePath = ConfigurationManager.AppSettings["GitExePath"];
    var gitRootFolder = ConfigurationManager.AppSettings["GitBaseFolder"];

    if (IsPost && Request.Form["save_config"] != null)
    {
        gitExePath = Request.Form["gitExePath"];
        gitRootFolder = Request.Form["gitRootFolder"];
        if (!File.Exists(gitExePath))
        {
            isValid = false;
            <p class="message error">@gitExePath does not exist.</p>
        }
        if (!Directory.Exists(gitRootFolder))
        {
            isValid = false;
            <p class="message error">@gitRootFolder does not exist.</p>
        }
        if (isValid)
        {
            var config = WebConfigurationManager.OpenWebConfiguration("~");
            var appsettings = (AppSettingsSection)config.GetSection("appSettings");
            appsettings.Settings["GitExePath"].Value = gitExePath;
            appsettings.Settings["GitBaseFolder"].Value = gitRootFolder;
            config.Save();
            <p class="message">Configuration saved.</p>
        }
    }   
       
    var projectName = Request.Form["projectName"];
   
    if (IsPost && Request.Form["create_project"] != null)
    {       
        try
        {

            Project.Create(WebSecurity.CurrentUserName, projectName);
            errorMessage = "New project: " + projectName + " created.";
        }
        catch (Exception ex)
        {
            errorMessage = ex.Message;
        }
    }
    
}
@section head {
<script src="@Href("~/Scripts/jQuery.tmpl.min.js")" type="text/javascript"></script>
}

<form method="post" action="">
<div class="span-15">
    <h2>Projects</h2>
    <div id="projectList"></div>
</div>
<div class="span-9 last">
    @if(!errorMessage.IsEmpty())
    {
        <div class="message error">@errorMessage</div>
    }
    
    <fieldset>
        <legend>Server Configuration</legend>
        <ol>
            <li>
             Please make sure <a href="http://code.google.com/p/msysgit/downloads/list">Git for Windows (1.7+)</a> is installed and enter the path of git executable.
                <label for="gitExePath">Git Executable Path:</label>
                <input type="text" id="gitExePath" name="gitExePath" value="@gitExePath"/> 
                <br /><br />
            </li>
            <li>
            Please enter your git repositories' root folder. It is the parent folder of all repositories.
                <label for="gitRootFolder">Git Repository Root Folder:</label> 
                <input type="text" id="gitRootFolder" name="gitRootFolder" value="@gitRootFolder"/>
            </li>
        </ol>
        <p class="form-actions">
            <input type="submit" name="save_config" value="Save" title="Save" />
        </p>
    </fieldset>
    
    <fieldset>
        <legend>Create New Project</legend>
        <ol>
            <li>
                <label for="projectName">Project Name:</label>
                <input type="text" id="projectName" name="projectName" value="@projectName"/> 
            </li>
        </ol>
        <p class="form-actions">
            <input type="submit" name="create_project" value="Create" title="Save" />
        </p>
    </fieldset>

</div>
</form>

<script id="projectTemplate" type="text/x-jquery-tmpl"> 
    <div class="project">
        <div class="project-name"><a href="/Repository/${Id}">${Name}</a></div>
        <div class="project-url span-13">@host${Id}.git</div>
        
    </div>
</script>

<script type="text/javascript">

    $(function () {

        $.ajax({
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            url: "/odata/Repositories",
            success: function (data) {
                var repos = data.d;
                for (var i = 0; i < repos.length; i++) {
                    repos[i].Id = repos[i].Id.replace(".", "/");
                }
                $("#projectTemplate").tmpl(repos).appendTo("#projectList");
            },
            error: function (xhr) { alert(xhr.responseText); }
        });

    });

</script>  